package com.qianjiangcloud.store;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Typeface;
import android.text.TextUtils;

import com.qianjiangcloud.store.activity.account.AccountLoginFragment;
import com.qianjiangcloud.store.http.JsonRequestBean.JsonRequestCallback;
import com.qianjiangcloud.store.http.JsonTask;
import com.qianjiangcloud.store.libary.cache.FileSystem.FileSystem;
import com.qianjiangcloud.store.libary.cache.ImageCachePool;
import com.qianjiangcloud.store.wxapi.Constants;

import org.json.JSONObject;

import java.net.CookieHandler;
import java.net.CookieManager;
import java.util.ArrayList;

import cn.sharesdk.framework.ShareSDK;
import cn.shopex.pay.WeChatPayHandle;
import httplib.VolleyHttpHelper;
import httplib.VolleyRequestQueue;


public class AgentApplication extends Application {
	private LoginedUser mLoginedUser;
//	private ImageLoader mAvatarLoader;

	public Bitmap mAvatarMask, mAvatarCover;

	private ArrayList<Activity> mRecentActivies = new ArrayList<Activity>();

	public JSONObject mOrderDetail;
	public boolean gotoMyFavorite = false;
	private Typeface typeface;
	public String payMoney;//充值金额
	@Override
	public void onCreate() {
		super.onCreate();
		// ShareSDK
		ShareSDK.initSDK(this, "8f5830089d1f");

		//初始化文件系统
		FileSystem.init(getApplicationContext());
		//初始化图片缓存
		ImageCachePool.initImageLoader(getApplicationContext());
		
		// 头像
		Resources resources = getResources();
		mAvatarMask = BitmapFactory.decodeResource(resources,
				R.drawable.account_avatar);
		mAvatarCover = BitmapFactory.decodeResource(resources,
				R.drawable.westore_avatar_hole);

		// 错误信息收集
		CrashHandler crashHandler = CrashHandler.getInstance();
		crashHandler.init(getApplicationContext());

		// cookie管理
		CookieManager cookieManager = null;
		cookieManager = new CookieManager(null, null);
		CookieHandler.setDefault(cookieManager);

		mLoginedUser = LoginedUser.getInstance();
		//mAvatarLoader = Run.getDefaultAvatarLoader(this, resources);
		
		this.userAutoLogin();
		//typeface = Typeface.createFromAsset(this.getAssets(), "fonts/huawenblackfont.ttf");

		WeChatPayHandle.createWXAPI(this, Constants.APP_ID);


		/**网络请求工具初始化*/
		VolleyRequestQueue.setApplicationContext(this);
		VolleyHttpHelper.setServiceURL(Run.API_URL);
		VolleyHttpHelper.setServiceToken(Run.TOKEN);
		
	}
	
	public Typeface getTypeface() {
        return typeface;
    }

	// 自动登录
	public void userAutoLogin() {
		if (!TextUtils.isEmpty(Run.loadOptionString(this,
				Run.pk_logined_username, Run.EMPTY_STR))
				&& !TextUtils.isEmpty(Run.loadOptionString(this,
						Run.pk_logined_user_password, Run.EMPTY_STR))) {
			Run.excuteJsonTask(
					new JsonTask(),
					new AccountLoginFragment.UserLoginTask(null, Run
							.loadOptionString(this, Run.pk_logined_username,
									Run.EMPTY_STR), Run.loadOptionString(this,
							Run.pk_logined_user_password, Run.EMPTY_STR), null,
							new JsonRequestCallback() {
								@Override
								public void task_response(String jsonStr) {
									userLoginCallback(jsonStr);
								}
							}));
		}
	}

	/**
	 * 用户登录返回
	 * 
	 * @param json_str
	 */
	private void userLoginCallback(String json_str) {
		try {
			JSONObject all = new JSONObject(json_str);
			if (Run.checkRequestJson(this, all, false)) {
				LoginedUser user = AgentApplication.getLoginedUser(this);
				user.setIsLogined(true);
				user.setUserInfo(all.getJSONObject("data"));
			}
		} catch (Exception e) {
		}
	}

	@Override
	public void onTerminate() {
		ShareSDK.stopSDK(this);
		mLoginedUser.clearLoginedStatus();
		super.onTerminate();
	}
//
//	public static ImageLoader getAvatarLoader(Context context) {
//		return getApp(context).mAvatarLoader;
//	}

	/**
	 * 正在查看的订单
	 * 
	 * @param mOrderDetail
	 */
	public void setOrderDetail(JSONObject mOrderDetail) {
		this.mOrderDetail = mOrderDetail;
	}

	/**
	 * 打开的Activity历史
	 * 
	 * @return
	 */
	public ArrayList<Activity> getRecentActivies() {
		return mRecentActivies;
	}

	/**
	 * 获取App代理
	 * 
	 * @param context
	 * @return
	 */
	public static AgentApplication getApp(Context context) {
		return (AgentApplication) context.getApplicationContext();
	}

	/**
	 * 登录的用户
	 * 
	 * @return
	 */
	public LoginedUser getLoginedUser() {
		return mLoginedUser;
	}

	/**
	 * 登录的用户
	 * 
	 * @return
	 */
	public static LoginedUser getLoginedUser(Context context) {
		return AgentApplication.getApp(context).getLoginedUser();
	}
}
